package com.a.lib_base.base.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import androidx.viewbinding.ViewBinding
import com.a.lib_base.R
import com.a.lib_base.able.IBaseBarController
import com.a.lib_base.databinding.ActivityBaseContainerBinding

/**
 * CREATE BY 黄炜强
 * 2022/7/27 09:13
 * Desc:
 * */
abstract class BaseFragmentViewBinding<VB : ViewBinding> : Fragment(), IBaseBarController {
    override fun isAddTitleBar(): Boolean {
        return false
    }

    override fun setTitle(): String? {
        return null
    }

    override fun setBarBackground(): Int {
        return 0
    }


    private var _b: VB? = null
    private var _baseContainerBinding: ActivityBaseContainerBinding? = null
    val b: VB
        get() = _b!!
    val baseContainerBinding
        get() = _baseContainerBinding!!

    abstract fun viewBinding(view: ViewGroup?): VB

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (isAddTitleBar) {
            _baseContainerBinding = ActivityBaseContainerBinding.inflate(layoutInflater)
            baseContainerBinding.root.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.white))
            baseContainerBinding.titleBar.imgBack.setOnClickListener { v -> remove() }
            baseContainerBinding.titleBar.tvTitle.text = setTitle()
            baseContainerBinding.titleBar.llCommonBar.setBackgroundColor(
                ContextCompat.getColor(
                    requireContext(),
                    setBarBackground()
                )
            )
            baseContainerBinding.root.isClickable = true//防止点击穿透
            _b = viewBinding(baseContainerBinding.root)
            b.root.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.white))
            return _baseContainerBinding?.root
        }
        return _b?.root
    }

     fun add(fragmentClass: Class<out Fragment?>?, layId: Int, bundle: Bundle?, tag: String?) {
        requireActivity().supportFragmentManager.beginTransaction()
            .setCustomAnimations(
                android.R.anim.slide_in_left,
                android.R.anim.slide_out_right,
                android.R.anim.fade_in,
                android.R.anim.slide_out_right
            )
            .addToBackStack(tag)
            .add(layId, fragmentClass!!, bundle, tag)
//            .hide(this)
            .commit()
    }

    fun add(fragment: Fragment, layId: Int) = requireActivity().supportFragmentManager.commit {
        setCustomAnimations(R.anim.right_in, R.anim.left_out)
        add(layId, fragment)
//        addToBackStack(null)
    }

//    fun remove() = requireActivity().supportFragmentManager.beginTransaction()
//        .setCustomAnimations(
//            0, R.anim.right_out
//        ).remove(this).commit()

    fun remove() {
        requireActivity().supportFragmentManager.popBackStack()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _b = null
        _baseContainerBinding = null
    }

}